package com.platform.controller;

import com.platform.entity.*;
import com.platform.service.*;
import com.platform.utils.PageUtils;
import com.platform.utils.Query;
import com.platform.utils.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhurusen
 * @email 524227263@qq.com
 * @date 2018-06-03 11:20:12
 */
@Controller
@RequestMapping("index")
public class IndexController {

    @Autowired
    private UserService userService;
    @Autowired
    private ApiCircleService circleService;
    @Autowired
    private ApiCircleBillService circleBillService;
    @Autowired
    private ApiCircleResService circleResService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private ApiCallBillService callBillService;

    /**
     * 探索点赞
     */
    @RequestMapping("/circleList")
    @RequiresPermissions("apiuser:list")
    @ResponseBody
    public R circleList(@RequestParam Map<String, Object> params) {
        params.put("type", 3);
        //查询列表数据
        Query query = new Query(params);
        List<ApiCircleBillVo> circleBillVos = circleBillService.selectList(query);
        List<Map<String, Object>> list = new ArrayList<>();
        for (ApiCircleBillVo info : circleBillVos) {
            Map<String, Object> map = new HashMap<>();
            ApiCircleVo apiCircleVo = circleService.selectOne(info.getCircleId());
            UserEntity userVo = userService.queryObject(apiCircleVo.getUserId());
            params = new HashMap<>();
            params.put("circleId", info.getCircleId());
            List<ApiCircleResVo> resVos = circleResService.selectList(params);
            map.put("id", info.getId());
            map.put("time", info.getCreateTime());
            map.put("content", apiCircleVo.getContent());
            map.put("zbName", userVo.getNickname());
            map.put("res", resVos);
            list.add(map);
        }
        int total = circleBillService.selectCount(query);
        PageUtils pageUtil = new PageUtils(list, total, query.getLimit(), query.getPage());
        return R.ok().put("page", pageUtil);
    }

    /**
     * 视频直播
     */
    @RequestMapping("/callList")
    @RequiresPermissions("apiuser:list")
    @ResponseBody
    public R callList(@RequestParam Map<String, Object> params) {
        Query query = new Query(params);
        List<ApiCallBillVo> callBillVos = callBillService.selectList(params);
        List<Map<String, Object>> list = new ArrayList<>();
        for (ApiCallBillVo info : callBillVos) {
            UserEntity userEntity = userService.queryObject(info.getTvAnchor());
            Map<String, Object> map = new HashMap<>();
            map.put("id", info.getId());
            map.put("time", info.getCreateTime());
            map.put("talkTimes", info.getTalkTime());
            map.put("zbName", userEntity.getNickname());
            list.add(map);
        }
        int total = callBillService.queryTotal(query);
        PageUtils pageUtil = new PageUtils(list, total, query.getLimit(), query.getPage());
        return R.ok().put("page", pageUtil);
    }

    /**
     * 全部订单
     */
    @RequestMapping("/orderList")
    @RequiresPermissions("apiuser:list")
    @ResponseBody
    public R orderList(@RequestParam Map<String, Object> params) {
        Query query = new Query(params);
        List<OrderEntity> orders = orderService.queryList(query);
        List<Map<String, Object>> list = new ArrayList<>();
        for (OrderEntity info : orders) {
            Map<String, Object> map = new HashMap<>();
            UserEntity userEntity = userService.queryObject(info.getZbUserId());
            map.put("id", info.getId());
            map.put("time", info.getCreateDate());
            map.put("muldName", info.getMouldName());
            map.put("zbName", userEntity.getNickname());
            map.put("total", info.getTotal());
            map.put("isComment", info.getCommentStatus() == 1 ? "已评论" : "未评论");
            list.add(map);
        }
        int total = orderService.queryTotal(query);
        PageUtils pageUtil = new PageUtils(list, total, query.getLimit(), query.getPage());
        return R.ok().put("page", pageUtil);
    }
}
